package com.store.framework.gui.view;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.store.framework.gui.model.CalcModel;

public class CalcView extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7389422044447729324L;
	private static final String INITIAL_VALUE = "1";
	private JTextField m_userInputTf = new JTextField(5);
	private JTextField m_totalTf = new JTextField(20);
	private JButton m_multiplyBtn = new JButton("Multiply");
	private JButton m_clearBtn = new JButton("Clear");
	private CalcModel m_model;
	
	CalcView(CalcModel model){
		this.m_model = model;
		this.m_model.setValue(INITIAL_VALUE);
		this.m_totalTf.setText(this.m_model.getValue());
		this.m_totalTf.setEditable(false);
		JPanel content = new JPanel();
		content.setLayout(new FlowLayout());
		content.add(new JLabel("Input"));
		content.add(this.m_userInputTf);
		content.add(this.m_multiplyBtn);
		content.add(new JLabel("Total"));
		content.add(this.m_totalTf);
		content.add(this.m_clearBtn);
		this.setContentPane(content);
		this.pack();
		this.setTitle("Simple Calc - MVC");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void reset(){
		this.m_totalTf.setText(INITIAL_VALUE);
		this.m_userInputTf.setText("");
	}
	public String getUserInput(){
		return this.m_userInputTf.getText();
	}
	public void setTotal(String newTotal){
		this.m_totalTf.setText(newTotal);
	}
	public void showError(String errMessage){
		JOptionPane.showMessageDialog(this, errMessage);
	}
	public void addMultiplyListener(ActionListener mal){
		this.m_multiplyBtn.addActionListener(mal);
	}
	public void addClearListener(ActionListener cal){
		this.m_clearBtn.addActionListener(cal);
	}
}


